package com.cellulam.framework.core.json;

import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.cellulam.framework.core.model.DataResult;
import com.cellulam.framework.core.utils.JacksonUtils;


public class DataResultJsonSerializer extends JsonSerializer<DataResult> {

    private static ObjectMapper mapper = null;
    private static ObjectWriter writer = null;

    static {
        mapper = JacksonUtils.createMapper();
        writer = mapper.writer().withoutAttribute("ok").withoutAttribute("fail").withoutAttribute("isOk").withoutAttribute("isFail");
    }

    @Override
    public void serialize(DataResult dr, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (dr != null) {
            gen.writeRawValue(writer.writeValueAsString(dr));
//            writer.writeValue(gen, dr);
        } else {
            gen.writeObject(dr);
        }
    }

}
